#! /bin/sh -e

PREFIX=/usr
BIN=$PREFIX/bin
MAN=$PREFIX/share/man/man1/

die()
{
    echo "$1" >&2
    echo "Installation aborted." >&2
    exit 1
}


echo "This script will install additional material for F# including"
echo "man pages, fsharpc and fsharpi scripts and Gtk# support for F#"
echo "Interactive (root access needed)"
echo ""

# ------------------------------------------------------------------------------
# Utility function that searches specified directories for a specified file
# and if the file is not found, it asks user to provide a directory

RESULT=""

searchpaths()
{
  declare -a DIRS=("${!3}")
  FILE=$2
  DIR=${DIRS[0]}
  for TRYDIR in ${DIRS[@]}
  do
    if [ -f $TRYDIR/$FILE ]
    then
      DIR=$TRYDIR
    fi
  done

  while [ ! -f $DIR/$FILE ]
  do
    echo "File '$FILE' was not found in any of ${DIRS[@]}. Please enter $1 installation directory:"
    read DIR
  done
  RESULT=$DIR
}

# ------------------------------------------------------------------------------
# Locate F# installation directory - this is needed, because we want to 
# add environment variable with it, generate 'fsharpc' and 'fsharpi' and also
# copy load-gtk.fsx to that directory
# ------------------------------------------------------------------------------

PATHS=( $1 /usr/lib/fsharp /usr/lib/shared/fsharp )
searchpaths "F# installation" FSharp.Core.dll PATHS[@]
FSHARPDIR=$RESULT
echo "Successfully found F# installation directory."

# ------------------------------------------------------------------------------
# Check that we have everything we need
# ------------------------------------------------------------------------------

[ $(id -u) -eq 0 ] || die "Please run the script as root."
which mono > /dev/null || die "mono not found in PATH."

# ------------------------------------------------------------------------------
# Make sure that all additional assemblies are in GAC
# ------------------------------------------------------------------------------

echo "Installing additional F# assemblies to the GAC"
gacutil -i $FSHARPDIR/FSharp.Build.dll
gacutil -i $FSHARPDIR/FSharp.Compiler.dll
gacutil -i $FSHARPDIR/FSharp.Compiler.Interactive.Settings.dll
gacutil -i $FSHARPDIR/FSharp.Compiler.Server.Shared.dll

# ------------------------------------------------------------------------------
# Install additional files
# ------------------------------------------------------------------------------

# Install man pages

echo "Installing additional F# commands, scripts and man pages"
mkdir -p $MAN
cp *.1 $MAN

# Export the FSHARP_COMPILER_BIN environment variable

if [[ ! "$OSTYPE" =~ "darwin" ]]; then
  echo "export FSHARP_COMPILER_BIN=$FSHARPDIR" > fsharp.sh
  mv fsharp.sh /etc/profile.d/
fi

# Generate 'load-gtk.fsx' script for F# Interactive (ask user if we cannot find binaries)

PATHS=( /usr/lib/mono/gtk-sharp-2.0 /usr/lib/cli/gtk-sharp-2.0 /Library/Frameworks/Mono.framework/Versions/2.8/lib/mono/gtk-sharp-2.0 )
searchpaths "Gtk#" gtk-sharp.dll PATHS[@]
GTKDIR=$RESULT
echo "Successfully found Gtk# root directory."

PATHS=( /usr/lib/mono/gtk-sharp-2.0 /usr/lib/cli/glib-sharp-2.0 /Library/Frameworks/Mono.framework/Versions/2.8/lib/mono/gtk-sharp-2.0 )
searchpaths "Glib" glib-sharp.dll PATHS[@]
GLIBDIR=$RESULT
echo "Successfully found Glib# root directory."

PATHS=( /usr/lib/mono/gtk-sharp-2.0 /usr/lib/cli/atk-sharp-2.0 /Library/Frameworks/Mono.framework/Versions/2.8/lib/mono/gtk-sharp-2.0 )
searchpaths "Atk#" atk-sharp.dll PATHS[@]
ATKDIR=$RESULT
echo "Successfully found Atk# root directory."

PATHS=( /usr/lib/mono/gtk-sharp-2.0 /usr/lib/cli/gdk-sharp-2.0 /Library/Frameworks/Mono.framework/Versions/2.8/lib/mono/gtk-sharp-2.0 )
searchpaths "Gdk#" gdk-sharp.dll PATHS[@]
GDKDIR=$RESULT
echo "Successfully found Gdk# root directory."

cp bonus/load-gtk.fsx load-gtk1.fsx
sed "s,INSERTGTKPATH,$GTKDIR,g" load-gtk1.fsx > load-gtk2.fsx
sed "s,INSERTGDKPATH,$GDKDIR,g" load-gtk2.fsx > load-gtk3.fsx
sed "s,INSERTATKPATH,$ATKDIR,g" load-gtk3.fsx > load-gtk4.fsx
sed "s,INSERTGLIBPATH,$GLIBDIR,g" load-gtk4.fsx > load-gtk.fsx
rm load-gtk1.fsx
rm load-gtk2.fsx
rm load-gtk3.fsx
rm load-gtk4.fsx
mv load-gtk.fsx $FSHARPDIR/load-gtk.fsx

# Generate 'fsharpc' and 'fsharpi' scripts (using the F# path)
# 'fsharpi' automatically searches F# root directory (e.g. load-gtk)

echo "#!/bin/sh" > fsharpc
echo "exec mono $FSHARPDIR/fsc.exe --resident \"\$@\"" >> fsharpc
chmod 755 fsharpc

echo "#!/bin/sh" > fsharpi
echo "exec mono $FSHARPDIR/fsi.exe -I:\"$FSHARPDIR\" \"\$@\"" >> fsharpi
chmod 755 fsharpi

mv fsharpc $BIN/fsharpc
mv fsharpi $BIN/fsharpi
